import styles from './styles.less';
import {Pagination, Table, Space, Tag, Modal} from 'antd';
import {useCallback, useEffect, useState} from 'react';
import {ExclamationCircleFilled} from '@ant-design/icons';
import {nowrapColumns} from '@/utils/utils';

import {connect, useAccess} from 'umi';

const DataTable = (props) => {
  const access = useAccess();
  const {dispatch, dataTable, loading, submitting} = props;
  const {payways, setDataForm, tablePayload, setTablePayload} = props.data;
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);

  /* 获取列表 */
  const fetchList = useCallback((payload) => {
    if (payload.param.mchCode === 0) {
      dispatch({
        type: 'PMchInfoPayways/chgDataTable',
        payload: {total: 0, list: []}
      });
    } else {
      dispatch({
        type: 'PMchInfoPayways/fetchLists',
        payload: payload,
      });
    }
  }, [dispatch]);

  /* 查询监听 */
  useEffect(() => {
    fetchList({...tablePayload, param: {mchCode: payways ? payways.data.code : 0}});
  }, [fetchList, tablePayload, payways]);

  /* 编辑 */
  function edit(e, record) {
    e.preventDefault();
    setDataForm({
      visible: true,
      title: '编辑操作',
      data: record,
    });
    return false;
  }

  /* 删除 */
  function del(e, record) {
    e.preventDefault();
    if (!submitting) {
      Modal.confirm({
        title: '提示',
        icon: <ExclamationCircleFilled/>,
        content: '是否确定删除数据？',
        onOk() {
          dispatch({
            type: 'PMchInfoPayways/del',
            payload: [record.id],
            callback: () => {
              setTablePayload({...tablePayload, pageNum: 1});
              setSelectedRowKeys([]);
            },
          });
        },
      });
    }
    return false;
  }

  const columns = [
    {
      title: '序号',
      width: 70,
      align: 'center',
      render: ({}, {}, index) => (
        <span>
          {(tablePayload.pageNum - 1) * tablePayload.pageSize + index + 1}
        </span>
      ),
    },
    {
      title: '支付方式名称',
      dataIndex: 'payWay',
      align: 'center',
      render: (_, record) => {
        return (
          <span>
            {record.payWay ? record.payWay.name : '-'}
          </span>
        );
      },
    },
    {
      title: '支付方式编号',
      dataIndex: 'wayCode',
      align: 'center',
      render: (_, record) => {
        return (
          <span>
            {record.payWay ? record.payWay.code : '-'}
          </span>
        );
      },
    },
    {
      title: '当前状态',
      dataIndex: 'status',
      align: 'center',
      render: (_, record) => {
        return (
          <Tag color={record.status === 1 ? 'geekblue' : 'volcano'}>
            {record.status === 1 ? '正常' : '停用'}
          </Tag>
        );
      },
    },
    {
      title: '排序属性',
      dataIndex: 'orderby',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'option',
      align: 'center',
      width: 120,
      render: (_, record) => (
        <Space size='small'>
          {access?.payWay_config_edit && (
            <a onClick={e => edit(e, record)}>{record.id ? '编辑' : '配置'}</a>
          )}
          {access?.payWay_config_del && (
            <a onClick={e => del(e, record)}>删除</a>
          )}
        </Space>
      ),
    },
  ];

  return (
    <>
      <div className={styles.myDataTable}>
        <Table
          rowSelection={{
            selectedRowKeys,
            onChange: (newSelectedRowKeys) => setSelectedRowKeys(newSelectedRowKeys),
          }}
          className={styles.myTable} bordered={false} size={'middle'} columns={nowrapColumns(columns, dataTable.list)}
          dataSource={dataTable.list} rowKey={'wayCode'}
          pagination={false} scroll={{y: '100%'}} loading={loading}
        />
      </div>
      <Pagination
        pageSize={tablePayload.pageSize} current={tablePayload.pageNum} total={dataTable.total}
        className={styles.myPagination} size={'default'} hideOnSinglePage={false}
        showTotal={(total) => {
          return `共 ${total} 条记录`;
        }}
        pageSizeOptions={[10, 30, 60, 120]}
        showSizeChanger showQuickJumper={true}
        onChange={(page, pageSize) => {
          setTablePayload({...tablePayload, pageNum: page, pageSize: pageSize});
        }}
      />
    </>

  );
};

export default connect(({loading, PMchInfoPayways}) => {
  return {
    loading: loading.effects['PMchInfoPayways/fetchLists'],
    submitting: loading.effects['PMchInfoPayways/del'],
    dataTable: PMchInfoPayways.dataTable,
  };
})(DataTable);
